1 module hip.jni.android.sensor; 2 import hip.jni.android.android_api; 3 4 /* 5 * Copyright (C) 2010 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 /** 21 * Structures and functions to receive and process sensor events in 22 * native code. 23 * 24 * @addtogroup Sensor 25 * @{ 26 */ 27 28 /** 29 * @file sensor.h 30 */ 31 32 /****************************************************************** 33 * 34 * IMPORTANT NOTICE: 35 * 36 * This file is part of Android's set of stable system headers 37 * exposed by the Android NDK (Native Development Kit). 38 * 39 * Third-party source AND binary code relies on the definitions 40 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES. 41 * 42 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES) 43 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS 44 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY 45 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES 46 */ 47 48 49 import hip.jni.android.looper; 50 import core.stdc.math; 51 import core.stdc.stdint; 52 53 54 extern(C): 55 struct AHardwareBuffer; 56 57 enum ASENSOR_RESOLUTION_INVALID() 58 { 59 return nanf(cast(char*)"\0".ptr); 60 } 61 enum ASENSOR_FIFO_COUNT_INVALID = -1; 62 enum ASENSOR_DELAY_INVALID = INT32_MIN; 63 enum ASENSOR_INVALID = -1; 64 65 /* (Keep in sync with hardware/sensors-base.h and Sensor.java.) */ 66 67 /** 68 * Sensor types. 69 * 70 * See 71 * [android.hardware.SensorEvent#values](https://developer.android.com/reference/android/hardware/SensorEvent.html#values) 72 * for detailed explanations of the data returned for each of these types. 73 */ 74 enum { 75 /** 76 * Invalid sensor type. Returned by {@link ASensor_getType} as error value. 77 */ 78 ASENSOR_TYPE_INVALID = -1, 79 /** 80 * {@link ASENSOR_TYPE_ACCELEROMETER} 81 * reporting-mode: continuous 82 * 83 * All values are in SI units (m/s^2) and measure the acceleration of the 84 * device minus the force of gravity. 85 */ 86 ASENSOR_TYPE_ACCELEROMETER = 1, 87 /** 88 * {@link ASENSOR_TYPE_MAGNETIC_FIELD} 89 * reporting-mode: continuous 90 * 91 * All values are in micro-Tesla (uT) and measure the geomagnetic 92 * field in the X, Y and Z axis. 93 */ 94 ASENSOR_TYPE_MAGNETIC_FIELD = 2, 95 /** 96 * {@link ASENSOR_TYPE_GYROSCOPE} 97 * reporting-mode: continuous 98 * 99 * All values are in radians/second and measure the rate of rotation 100 * around the X, Y and Z axis. 101 */ 102 ASENSOR_TYPE_GYROSCOPE = 4, 103 /** 104 * {@link ASENSOR_TYPE_LIGHT} 105 * reporting-mode: on-change 106 * 107 * The light sensor value is returned in SI lux units. 108 */ 109 ASENSOR_TYPE_LIGHT = 5, 110 /** 111 * {@link ASENSOR_TYPE_PRESSURE} 112 * 113 * The pressure sensor value is returned in hPa (millibar). 114 */ 115 ASENSOR_TYPE_PRESSURE = 6, 116 /** 117 * {@link ASENSOR_TYPE_PROXIMITY} 118 * reporting-mode: on-change 119 * 120 * The proximity sensor which turns the screen off and back on during calls is the 121 * wake-up proximity sensor. Implement wake-up proximity sensor before implementing 122 * a non wake-up proximity sensor. For the wake-up proximity sensor set the flag 123 * SENSOR_FLAG_WAKE_UP. 124 * The value corresponds to the distance to the nearest object in centimeters. 125 */ 126 ASENSOR_TYPE_PROXIMITY = 8, 127 /** 128 * {@link ASENSOR_TYPE_GRAVITY} 129 * 130 * All values are in SI units (m/s^2) and measure the direction and 131 * magnitude of gravity. When the device is at rest, the output of 132 * the gravity sensor should be identical to that of the accelerometer. 133 */ 134 ASENSOR_TYPE_GRAVITY = 9, 135 /** 136 * {@link ASENSOR_TYPE_LINEAR_ACCELERATION} 137 * reporting-mode: continuous 138 * 139 * All values are in SI units (m/s^2) and measure the acceleration of the 140 * device not including the force of gravity. 141 */ 142 ASENSOR_TYPE_LINEAR_ACCELERATION = 10, 143 /** 144 * {@link ASENSOR_TYPE_ROTATION_VECTOR} 145 */ 146 ASENSOR_TYPE_ROTATION_VECTOR = 11, 147 /** 148 * {@link ASENSOR_TYPE_RELATIVE_HUMIDITY} 149 * 150 * The relative humidity sensor value is returned in percent. 151 */ 152 ASENSOR_TYPE_RELATIVE_HUMIDITY = 12, 153 /** 154 * {@link ASENSOR_TYPE_AMBIENT_TEMPERATURE} 155 * 156 * The ambient temperature sensor value is returned in Celcius. 157 */ 158 ASENSOR_TYPE_AMBIENT_TEMPERATURE = 13, 159 /** 160 * {@link ASENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED} 161 */ 162 ASENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED = 14, 163 /** 164 * {@link ASENSOR_TYPE_GAME_ROTATION_VECTOR} 165 */ 166 ASENSOR_TYPE_GAME_ROTATION_VECTOR = 15, 167 /** 168 * {@link ASENSOR_TYPE_GYROSCOPE_UNCALIBRATED} 169 */ 170 ASENSOR_TYPE_GYROSCOPE_UNCALIBRATED = 16, 171 /** 172 * {@link ASENSOR_TYPE_SIGNIFICANT_MOTION} 173 */ 174 ASENSOR_TYPE_SIGNIFICANT_MOTION = 17, 175 /** 176 * {@link ASENSOR_TYPE_STEP_DETECTOR} 177 */ 178 ASENSOR_TYPE_STEP_DETECTOR = 18, 179 /** 180 * {@link ASENSOR_TYPE_STEP_COUNTER} 181 */ 182 ASENSOR_TYPE_STEP_COUNTER = 19, 183 /** 184 * {@link ASENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR} 185 */ 186 ASENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR = 20, 187 /** 188 * {@link ASENSOR_TYPE_HEART_RATE} 189 */ 190 ASENSOR_TYPE_HEART_RATE = 21, 191 /** 192 * {@link ASENSOR_TYPE_POSE_6DOF} 193 */ 194 ASENSOR_TYPE_POSE_6DOF = 28, 195 /** 196 * {@link ASENSOR_TYPE_STATIONARY_DETECT} 197 */ 198 ASENSOR_TYPE_STATIONARY_DETECT = 29, 199 /** 200 * {@link ASENSOR_TYPE_MOTION_DETECT} 201 */ 202 ASENSOR_TYPE_MOTION_DETECT = 30, 203 /** 204 * {@link ASENSOR_TYPE_HEART_BEAT} 205 */ 206 ASENSOR_TYPE_HEART_BEAT = 31, 207 /** 208 * This sensor type is for delivering additional sensor information aside 209 * from sensor event data. 210 * 211 * Additional information may include: 212 * - {@link ASENSOR_ADDITIONAL_INFO_INTERNAL_TEMPERATURE} 213 * - {@link ASENSOR_ADDITIONAL_INFO_SAMPLING} 214 * - {@link ASENSOR_ADDITIONAL_INFO_SENSOR_PLACEMENT} 215 * - {@link ASENSOR_ADDITIONAL_INFO_UNTRACKED_DELAY} 216 * - {@link ASENSOR_ADDITIONAL_INFO_VEC3_CALIBRATION} 217 * 218 * This type will never bind to a sensor. In other words, no sensor in the 219 * sensor list can have the type {@link ASENSOR_TYPE_ADDITIONAL_INFO}. 220 * 221 * If a device supports the sensor additional information feature, it will 222 * report additional information events via {@link ASensorEvent} and will 223 * have {@link ASensorEvent#type} set to 224 * {@link ASENSOR_TYPE_ADDITIONAL_INFO} and {@link ASensorEvent#sensor} set 225 * to the handle of the reporting sensor. 226 * 227 * Additional information reports consist of multiple frames ordered by 228 * {@link ASensorEvent#timestamp}. The first frame in the report will have 229 * a {@link AAdditionalInfoEvent#type} of 230 * {@link ASENSOR_ADDITIONAL_INFO_BEGIN}, and the last frame in the report 231 * will have a {@link AAdditionalInfoEvent#type} of 232 * {@link ASENSOR_ADDITIONAL_INFO_END}. 233 * 234 */ 235 ASENSOR_TYPE_ADDITIONAL_INFO = 33, 236 /** 237 * {@link ASENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT} 238 */ 239 ASENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT = 34, 240 /** 241 * {@link ASENSOR_TYPE_ACCELEROMETER_UNCALIBRATED} 242 */ 243 ASENSOR_TYPE_ACCELEROMETER_UNCALIBRATED = 35, 244 /** 245 * {@link ASENSOR_TYPE_HINGE_ANGLE} 246 * reporting-mode: on-change 247 * 248 * The hinge angle sensor value is returned in degrees. 249 */ 250 ASENSOR_TYPE_HINGE_ANGLE = 36, 251 } 252 253 /** 254 * Sensor accuracy measure. 255 */ 256 enum { 257 /** no contact */ 258 ASENSOR_STATUS_NO_CONTACT = -1, 259 /** unreliable */ 260 ASENSOR_STATUS_UNRELIABLE = 0, 261 /** low accuracy */ 262 ASENSOR_STATUS_ACCURACY_LOW = 1, 263 /** medium accuracy */ 264 ASENSOR_STATUS_ACCURACY_MEDIUM = 2, 265 /** high accuracy */ 266 ASENSOR_STATUS_ACCURACY_HIGH = 3 267 } 268 269 /** 270 * Sensor Reporting Modes. 271 */ 272 enum { 273 /** invalid reporting mode */ 274 AREPORTING_MODE_INVALID = -1, 275 /** continuous reporting */ 276 AREPORTING_MODE_CONTINUOUS = 0, 277 /** reporting on change */ 278 AREPORTING_MODE_ON_CHANGE = 1, 279 /** on shot reporting */ 280 AREPORTING_MODE_ONE_SHOT = 2, 281 /** special trigger reporting */ 282 AREPORTING_MODE_SPECIAL_TRIGGER = 3 283 } 284 285 /** 286 * Sensor Direct Report Rates. 287 */ 288 enum { 289 /** stopped */ 290 ASENSOR_DIRECT_RATE_STOP = 0, 291 /** nominal 50Hz */ 292 ASENSOR_DIRECT_RATE_NORMAL = 1, 293 /** nominal 200Hz */ 294 ASENSOR_DIRECT_RATE_FAST = 2, 295 /** nominal 800Hz */ 296 ASENSOR_DIRECT_RATE_VERY_FAST = 3 297 } 298 299 /** 300 * Sensor Direct Channel Type. 301 */ 302 enum { 303 /** shared memory created by ASharedMemory_create */ 304 ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY = 1, 305 /** AHardwareBuffer */ 306 ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER = 2 307 } 308 309 /** 310 * Sensor Additional Info Types. 311 * 312 * Used to populate {@link AAdditionalInfoEvent#type}. 313 */ 314 enum { 315 /** Marks the beginning of additional information frames */ 316 ASENSOR_ADDITIONAL_INFO_BEGIN = 0, 317 318 /** Marks the end of additional information frames */ 319 ASENSOR_ADDITIONAL_INFO_END = 1, 320 321 /** 322 * Estimation of the delay that is not tracked by sensor timestamps. This 323 * includes delay introduced by sensor front-end filtering, data transport, 324 * etc. 325 * float[2]: delay in seconds, standard deviation of estimated value 326 */ 327 ASENSOR_ADDITIONAL_INFO_UNTRACKED_DELAY = 0x10000, 328 329 /** float: Celsius temperature */ 330 ASENSOR_ADDITIONAL_INFO_INTERNAL_TEMPERATURE, 331 332 /** 333 * First three rows of a homogeneous matrix, which represents calibration to 334 * a three-element vector raw sensor reading. 335 * float[12]: 3x4 matrix in row major order 336 */ 337 ASENSOR_ADDITIONAL_INFO_VEC3_CALIBRATION, 338 339 /** 340 * Location and orientation of sensor element in the device frame: origin is 341 * the geometric center of the mobile device screen surface; the axis 342 * definition corresponds to Android sensor definitions. 343 * float[12]: 3x4 matrix in row major order 344 */ 345 ASENSOR_ADDITIONAL_INFO_SENSOR_PLACEMENT, 346 347 /** 348 * float[2]: raw sample period in seconds, 349 * standard deviation of sampling period 350 */ 351 ASENSOR_ADDITIONAL_INFO_SAMPLING, 352 }; 353 354 /* 355 * A few useful constants 356 */ 357 358 /** Earth's gravity in m/s^2 */ 359 enum ASENSOR_STANDARD_GRAVITY = 9.80665f; 360 /** Maximum magnetic field on Earth's surface in uT */ 361 enum ASENSOR_MAGNETIC_FIELD_EARTH_MAX = 60.0f; 362 /** Minimum magnetic field on Earth's surface in uT*/ 363 enum ASENSOR_MAGNETIC_FIELD_EARTH_MIN = 30.0f; 364 365 /** 366 * A sensor event. 367 */ 368 369 /* NOTE: changes to these structs have to be backward compatible */ 370 struct ASensorVector { 371 union { 372 float[3] v; 373 struct { 374 float x; 375 float y; 376 float z; 377 } 378 struct { 379 float azimuth; 380 float pitch; 381 float roll; 382 } 383 } 384 int8_t status; 385 uint8_t[3] reserved; 386 } 387 388 struct AMetaDataEvent { 389 int32_t what; 390 int32_t sensor; 391 } 392 393 struct AUncalibratedEvent { 394 union { 395 float[3] uncalib; 396 struct { 397 float x_uncalib; 398 float y_uncalib; 399 float z_uncalib; 400 } 401 } 402 union { 403 float[3] bias; 404 struct { 405 float x_bias; 406 float y_bias; 407 float z_bias; 408 } 409 } 410 } 411 412 struct AHeartRateEvent { 413 float bpm; 414 int8_t status; 415 } 416 417 struct ADynamicSensorEvent { 418 int32_t connected; 419 int32_t handle; 420 } 421 422 struct AAdditionalInfoEvent { 423 int32_t type; 424 int32_t serial; 425 union { 426 int32_t[14] data_int32; 427 float[14] data_float; 428 } 429 } 430 431 /* NOTE: changes to this struct has to be backward compatible */ 432 struct ASensorEvent { 433 int32_t version_; /* sizeof(struct ASensorEvent) */ 434 int32_t sensor; 435 int32_t type; 436 int32_t reserved0; 437 int64_t timestamp; 438 union { 439 union { 440 float[16] data; 441 ASensorVector vector; 442 ASensorVector acceleration; 443 ASensorVector magnetic; 444 float temperature; 445 float distance; 446 float light; 447 float pressure; 448 float relative_humidity; 449 AUncalibratedEvent uncalibrated_gyro; 450 AUncalibratedEvent uncalibrated_magnetic; 451 AMetaDataEvent meta_data; 452 AHeartRateEvent heart_rate; 453 ADynamicSensorEvent dynamic_sensor_meta; 454 AAdditionalInfoEvent additional_info; 455 } 456 union u64 { 457 uint64_t[8] data; 458 uint64_t step_counter; 459 } 460 } 461 462 uint32_t flags; 463 int32_t[3] reserved1; 464 } 465 466 /** 467 * {@link ASensorManager} is an opaque type to manage sensors and 468 * events queues. 469 * 470 * {@link ASensorManager} is a singleton that can be obtained using 471 * ASensorManager_getInstance(). 472 * 473 * This file provides a set of functions that uses {@link 474 * ASensorManager} to access and list hardware sensors, and 475 * create and destroy event queues: 476 * - ASensorManager_getSensorList() 477 * - ASensorManager_getDefaultSensor() 478 * - ASensorManager_getDefaultSensorEx() 479 * - ASensorManager_createEventQueue() 480 * - ASensorManager_destroyEventQueue() 481 */ 482 struct ASensorManager; 483 484 485 /** 486 * {@link ASensorEventQueue} is an opaque type that provides access to 487 * {@link ASensorEvent} from hardware sensors. 488 * 489 * A new {@link ASensorEventQueue} can be obtained using ASensorManager_createEventQueue(). 490 * 491 * This file provides a set of functions to enable and disable 492 * sensors, check and get events, and set event rates on a {@link 493 * ASensorEventQueue}. 494 * - ASensorEventQueue_enableSensor() 495 * - ASensorEventQueue_disableSensor() 496 * - ASensorEventQueue_hasEvents() 497 * - ASensorEventQueue_getEvents() 498 * - ASensorEventQueue_setEventRate() 499 * - ASensorEventQueue_requestAdditionalInfoEvents() 500 */ 501 struct ASensorEventQueue; 502 503 /** 504 * {@link ASensor} is an opaque type that provides information about 505 * an hardware sensors. 506 * 507 * A {@link ASensor} pointer can be obtained using 508 * ASensorManager_getDefaultSensor(), 509 * ASensorManager_getDefaultSensorEx() or from a {@link ASensorList}. 510 * 511 * This file provides a set of functions to access properties of a 512 * {@link ASensor}: 513 * - ASensor_getName() 514 * - ASensor_getVendor() 515 * - ASensor_getType() 516 * - ASensor_getResolution() 517 * - ASensor_getMinDelay() 518 * - ASensor_getFifoMaxEventCount() 519 * - ASensor_getFifoReservedEventCount() 520 * - ASensor_getStringType() 521 * - ASensor_getReportingMode() 522 * - ASensor_isWakeUpSensor() 523 * - ASensor_getHandle() 524 */ 525 struct ASensor; 526 /** 527 * {@link ASensorRef} is a type for constant pointers to {@link ASensor}. 528 * 529 * This is used to define entry in {@link ASensorList} arrays. 530 */ 531 alias ASensorRef = const ASensor*; 532 /** 533 * {@link ASensorList} is an array of reference to {@link ASensor}. 534 * 535 * A {@link ASensorList} can be initialized using ASensorManager_getSensorList(). 536 */ 537 alias ASensorList = const ASensorRef*; 538 539 /*****************************************************************************/ 540 541 /** 542 * Get a reference to the sensor manager. ASensorManager is a singleton 543 * per package as different packages may have access to different sensors. 544 * 545 * Deprecated: Use ASensorManager_getInstanceForPackage(const char*) instead. 546 * 547 * Example: 548 * 549 * ASensorManager* sensorManager = ASensorManager_getInstance(); 550 * 551 */ 552 static if(__ANDROID_API__ >= 26) 553 deprecated ASensorManager* ASensorManager_getInstance(); 554 else 555 ASensorManager* ASensorManager_getInstance(); 556 557 558 static if(__ANDROID_API__ >= 26) 559 { 560 /** 561 * Get a reference to the sensor manager. ASensorManager is a singleton 562 * per package as different packages may have access to different sensors. 563 * 564 * Example: 565 * 566 * ASensorManager* sensorManager = ASensorManager_getInstanceForPackage("foo.bar.baz"); 567 * 568 * Available since API level 26. 569 */ 570 ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName); 571 } 572 573 /** 574 * Returns the list of available sensors. 575 */ 576 int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list); 577 578 /** 579 * Returns the default sensor for the given type, or NULL if no sensor 580 * of that type exists. 581 */ 582 const(ASensor)* ASensorManager_getDefaultSensor(ASensorManager* manager, int type); 583 584 static if(__ANDROID_API__ >= 21) 585 { 586 /** 587 * Returns the default sensor with the given type and wakeUp properties or NULL if no sensor 588 * of this type and wakeUp properties exists. 589 * 590 * Available since API level 21. 591 */ 592 const (ASensor)* ASensorManager_getDefaultSensorEx(ASensorManager* manager, int type, bool wakeUp); 593 } 594 595 /** 596 * Creates a new sensor event queue and associate it with a looper. 597 * 598 * "ident" is a identifier for the events that will be returned when 599 * calling ALooper_pollOnce(). The identifier must be >= 0, or 600 * ALOOPER_POLL_CALLBACK if providing a non-NULL callback. 601 */ 602 ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager, 603 ALooper* looper, int ident, ALooper_callbackFunc callback, void* data); 604 605 /** 606 * Destroys the event queue and free all resources associated to it. 607 */ 608 int ASensorManager_destroyEventQueue(ASensorManager* manager, ASensorEventQueue* queue); 609 610 static if(__ANDROID_API__ >= 26) 611 { 612 613 /** 614 * Create direct channel based on shared memory 615 * 616 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY} to be used 617 * for configuring sensor direct report. 618 * 619 * Available since API level 26. 620 * 621 * \param manager the {@link ASensorManager} instance obtained from 622 * {@link ASensorManager_getInstanceForPackage}. 623 * \param fd file descriptor representing a shared memory created by 624 * {@link ASharedMemory_create} 625 * \param size size to be used, must be less or equal to size of shared memory. 626 * 627 * \return a positive integer as a channel id to be used in 628 * {@link ASensorManager_destroyDirectChannel} and 629 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures. 630 */ 631 int ASensorManager_createSharedMemoryDirectChannel(ASensorManager* manager, int fd, size_t size); 632 633 /** 634 * Create direct channel based on AHardwareBuffer 635 * 636 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER} type to be used 637 * for configuring sensor direct report. 638 * 639 * Available since API level 26. 640 * 641 * \param manager the {@link ASensorManager} instance obtained from 642 * {@link ASensorManager_getInstanceForPackage}. 643 * \param buffer {@link AHardwareBuffer} instance created by {@link AHardwareBuffer_allocate}. 644 * \param size the intended size to be used, must be less or equal to size of buffer. 645 * 646 * \return a positive integer as a channel id to be used in 647 * {@link ASensorManager_destroyDirectChannel} and 648 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures. 649 */ 650 int ASensorManager_createHardwareBufferDirectChannel( 651 ASensorManager* manager, const AHardwareBuffer* buffer, size_t size); 652 653 /** 654 * Destroy a direct channel 655 * 656 * Destroy a direct channel previously created using {@link ASensorManager_createDirectChannel}. 657 * The buffer used for creating direct channel does not get destroyed with 658 * {@link ASensorManager_destroy} and has to be close or released separately. 659 * 660 * Available since API level 26. 661 * 662 * \param manager the {@link ASensorManager} instance obtained from 663 * {@link ASensorManager_getInstanceForPackage}. 664 * \param channelId channel id (a positive integer) returned from 665 * {@link ASensorManager_createSharedMemoryDirectChannel} or 666 * {@link ASensorManager_createHardwareBufferDirectChannel}. 667 */ 668 void ASensorManager_destroyDirectChannel(ASensorManager* manager, int channelId); 669 670 /** 671 * Configure direct report on channel 672 * 673 * Configure sensor direct report on a direct channel: set rate to value other than 674 * {@link ASENSOR_DIRECT_RATE_STOP} so that sensor event can be directly 675 * written into the shared memory region used for creating the buffer. It returns a positive token 676 * which can be used for identify sensor events from different sensors on success. Calling with rate 677 * {@link ASENSOR_DIRECT_RATE_STOP} will stop direct report of the sensor specified in the channel. 678 * 679 * To stop all active sensor direct report configured to a channel, set sensor to NULL and rate to 680 * {@link ASENSOR_DIRECT_RATE_STOP}. 681 * 682 * In order to successfully configure a direct report, the sensor has to support the specified rate 683 * and the channel type, which can be checked by {@link ASensor_getHighestDirectReportRateLevel} and 684 * {@link ASensor_isDirectChannelTypeSupported}, respectively. 685 * 686 * Example: 687 * 688 * ASensorManager *manager = ...; 689 * ASensor *sensor = ...; 690 * int channelId = ...; 691 * 692 * ASensorManager_configureDirectReport(manager, sensor, channel_id, ASENSOR_DIRECT_RATE_FAST); 693 * 694 * Available since API level 26. 695 * 696 * \param manager the {@link ASensorManager} instance obtained from 697 * {@link ASensorManager_getInstanceForPackage}. 698 * \param sensor a {@link ASensor} to denote which sensor to be operate. It can be NULL if rate 699 * is {@link ASENSOR_DIRECT_RATE_STOP}, denoting stopping of all active sensor 700 * direct report. 701 * \param channelId channel id (a positive integer) returned from 702 * {@link ASensorManager_createSharedMemoryDirectChannel} or 703 * {@link ASensorManager_createHardwareBufferDirectChannel}. 704 * 705 * \return positive token for success or negative error code. 706 */ 707 int ASensorManager_configureDirectReport(ASensorManager* manager, 708 const ASensor* sensor, int channelId, int rate); 709 } 710 711 /*****************************************************************************/ 712 713 /** 714 * Enable the selected sensor with sampling and report parameters 715 * 716 * Enable the selected sensor at a specified sampling period and max batch report latency. 717 * To disable sensor, use {@link ASensorEventQueue_disableSensor}. 718 * 719 * \param queue {@link ASensorEventQueue} for sensor event to be report to. 720 * \param sensor {@link ASensor} to be enabled. 721 * \param samplingPeriodUs sampling period of sensor in microseconds. 722 * \param maxBatchReportLatencyus maximum time interval between two batch of sensor events are 723 * delievered in microseconds. For sensor streaming, set to 0. 724 * \return 0 on success or a negative error code on failure. 725 */ 726 int ASensorEventQueue_registerSensor(ASensorEventQueue* queue, const ASensor* sensor, 727 int32_t samplingPeriodUs, int64_t maxBatchReportLatencyUs); 728 729 /** 730 * Enable the selected sensor at default sampling rate. 731 * 732 * Start event reports of a sensor to specified sensor event queue at a default rate. 733 * 734 * \param queue {@link ASensorEventQueue} for sensor event to be report to. 735 * \param sensor {@link ASensor} to be enabled. 736 * 737 * \return 0 on success or a negative error code on failure. 738 */ 739 int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, const ASensor* sensor); 740 741 /** 742 * Disable the selected sensor. 743 * 744 * Stop event reports from the sensor to specified sensor event queue. 745 * 746 * \param queue {@link ASensorEventQueue} to be changed 747 * \param sensor {@link ASensor} to be disabled 748 * \return 0 on success or a negative error code on failure. 749 */ 750 int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, const ASensor* sensor); 751 752 /** 753 * Sets the delivery rate of events in microseconds for the given sensor. 754 * 755 * This function has to be called after {@link ASensorEventQueue_enableSensor}. 756 * Note that this is a hint only, generally event will arrive at a higher 757 * rate. It is an error to set a rate inferior to the value returned by 758 * ASensor_getMinDelay(). 759 * 760 * \param queue {@link ASensorEventQueue} to which sensor event is delivered. 761 * \param sensor {@link ASensor} of which sampling rate to be updated. 762 * \param usec sensor sampling period (1/sampling rate) in microseconds 763 * \return 0 on sucess or a negative error code on failure. 764 */ 765 int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, const ASensor* sensor, int32_t usec); 766 767 /** 768 * Determine if a sensor event queue has pending event to be processed. 769 * 770 * \param queue {@link ASensorEventQueue} to be queried 771 * \return 1 if the queue has events; 0 if it does not have events; 772 * or a negative value if there is an error. 773 */ 774 int ASensorEventQueue_hasEvents(ASensorEventQueue* queue); 775 776 /** 777 * Retrieve pending events in sensor event queue 778 * 779 * Retrieve next available events from the queue to a specified event array. 780 * 781 * \param queue {@link ASensorEventQueue} to get events from 782 * \param events pointer to an array of {@link ASensorEvents}. 783 * \param count max number of event that can be filled into array event. 784 * \return number of events returned on success; negative error code when 785 * no events are pending or an error has occurred. 786 * 787 * Examples: 788 * 789 * ASensorEvent event; 790 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, &event, 1); 791 * 792 * ASensorEvent eventBuffer[8]; 793 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, eventBuffer, 8); 794 * 795 */ 796 size_t ASensorEventQueue_getEvents(ASensorEventQueue* queue, ASensorEvent* events, size_t count); 797 798 static if(__ANDROID_API__ >= 29) 799 { 800 /** 801 * Request that {@link ASENSOR_TYPE_ADDITIONAL_INFO} events to be delivered on 802 * the given {@link ASensorEventQueue}. 803 * 804 * Sensor data events are always delivered to the {@ASensorEventQueue}. 805 * 806 * The {@link ASENSOR_TYPE_ADDITIONAL_INFO} events will be returned through 807 * {@link ASensorEventQueue_getEvents}. The client is responsible for checking 808 * {@link ASensorEvent#type} to determine the event type prior to handling of 809 * the event. 810 * 811 * The client must be tolerant of any value for 812 * {@link AAdditionalInfoEvent#type}, as new values may be defined in the future 813 * and may delivered to the client. 814 * 815 * Available since API level 29. 816 * 817 * \param queue {@link ASensorEventQueue} to configure 818 * \param enable true to request {@link ASENSOR_TYPE_ADDITIONAL_INFO} events, 819 * false to stop receiving events 820 * \return 0 on success or a negative error code on failure 821 */ 822 int ASensorEventQueue_requestAdditionalInfoEvents(ASensorEventQueue* queue, bool enable); 823 824 } 825 826 /*****************************************************************************/ 827 828 /** 829 * Returns this sensor's name (non localized) 830 */ 831 const(char)* ASensor_getName(const ASensor* sensor); 832 833 /** 834 * Returns this sensor's vendor's name (non localized) 835 */ 836 const(char)* ASensor_getVendor(const ASensor* sensor); 837 838 /** 839 * Return this sensor's type 840 */ 841 int ASensor_getType(const ASensor* sensor); 842 843 /** 844 * Returns this sensors's resolution 845 */ 846 float ASensor_getResolution(const ASensor* sensor); 847 848 /** 849 * Returns the minimum delay allowed between events in microseconds. 850 * A value of zero means that this sensor doesn't report events at a 851 * constant rate, but rather only when a new data is available. 852 */ 853 int ASensor_getMinDelay(const ASensor* sensor); 854 855 static if(__ANDROID_API__ >= 21) 856 { 857 /** 858 * Returns the maximum size of batches for this sensor. Batches will often be 859 * smaller, as the hardware fifo might be used for other sensors. 860 * 861 * Available since API level 21. 862 */ 863 int ASensor_getFifoMaxEventCount(const ASensor* sensor); 864 865 /** 866 * Returns the hardware batch fifo size reserved to this sensor. 867 * 868 * Available since API level 21. 869 */ 870 int ASensor_getFifoReservedEventCount(const ASensor* sensor); 871 872 /** 873 * Returns this sensor's string type. 874 * 875 * Available since API level 21. 876 */ 877 const (char)* ASensor_getStringType(const ASensor* sensor); 878 879 /** 880 * Returns the reporting mode for this sensor. One of AREPORTING_MODE_* constants. 881 * 882 * Available since API level 21. 883 */ 884 int ASensor_getReportingMode(const ASensor* sensor); 885 886 /** 887 * Returns true if this is a wake up sensor, false otherwise. 888 * 889 * Available since API level 21. 890 */ 891 bool ASensor_isWakeUpSensor(const ASensor* sensor); 892 893 } 894 895 static if(__ANDROID_API__ >= 26) 896 { 897 /** 898 * Test if sensor supports a certain type of direct channel. 899 * 900 * Available since API level 26. 901 * 902 * \param sensor a {@link ASensor} to denote the sensor to be checked. 903 * \param channelType Channel type constant, either 904 * {@ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY} 905 * or {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER}. 906 * \returns true if sensor supports the specified direct channel type. 907 */ 908 bool ASensor_isDirectChannelTypeSupported(const ASensor* sensor, int channelType); 909 910 /** 911 * Get the highest direct rate level that a sensor supports. 912 * 913 * Available since API level 26. 914 * 915 * \param sensor a {@link ASensor} to denote the sensor to be checked. 916 * 917 * \return a ASENSOR_DIRECT_RATE_... enum denoting the highest rate level supported by the sensor. 918 * If return value is {@link ASENSOR_DIRECT_RATE_STOP}, it means the sensor 919 * does not support direct report. 920 */ 921 int ASensor_getHighestDirectReportRateLevel(const ASensor* sensor); 922 } 923 924 static if(__ANDROID_API__ >= 29) 925 { 926 /** 927 * Returns the sensor's handle. 928 * 929 * The handle identifies the sensor within the system and is included in the 930 * {@link ASensorEvent#sensor} field of sensor events, including those sent with type 931 * {@link ASENSOR_TYPE_ADDITIONAL_INFO}. 932 * 933 * A sensor's handle is able to be used to map {@link ASENSOR_TYPE_ADDITIONAL_INFO} events to the 934 * sensor that generated the event. 935 * 936 * It is important to note that the value returned by {@link ASensor_getHandle} is not the same as 937 * the value returned by the Java API {@link android.hardware.Sensor#getId} and no mapping exists 938 * between the values. 939 * 940 * Available since API level 29. 941 */ 942 int ASensor_getHandle(const ASensor* sensor); 943 }